package cn.xmstore.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author： Aeolia
 * @Description： TODO
 * @Date： 2021/9/1 15:52
 */
@Component(value = "redisUtil")
public class RedisUtilImpl<H, K, V> implements RedisUtil<H, K, V> {

    @Resource(name = "redisTemplate")
    private RedisTemplate<Object,Object> redisTemplate;


    @Override
    public void add(K key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void add(K key, String value, long l) {
        redisTemplate.opsForValue().set(key, value, l,TimeUnit.SECONDS);
    }

    @Override
    public void addObject(H objectKey, K key, V value) {
        redisTemplate.opsForHash().put(objectKey, key, value);
    }

    @Override
    public void addObject(H objectKey, K key, V value, long l) {
        redisTemplate.boundHashOps(objectKey).put(key, value);
        redisTemplate.boundHashOps(objectKey).expire(l,TimeUnit.SECONDS);
        System.out.println(redisTemplate.expire(key, l, TimeUnit.SECONDS));
        System.out.println(redisTemplate.getExpire(key, TimeUnit.SECONDS));
    }

    @Override
    public void delete(K key) {

    }

    @Override
    public void delete(List<K> keyList) {

    }

    @Override
    public void delete(H objectKey, K key) {

    }

    @Override
    public void update(K key, String value) {

    }

    @Override
    public void updateObject(H objectKey, K key, V object) {

    }

    @Override
    public void updateObject(H objectKey, K key, V value, long l) {

    }

    @Override
    public V get(K key) {
        return (V) redisTemplate.opsForValue().get(key);
    }

    @Override
    public V getObject(H objectKey, K key) {
        return (V) redisTemplate.opsForHash().get(objectKey, key);
    }
}
